import React, { useRef, useState } from 'react';
import { format } from '@/utils/datetime';
import DataTable from '@/components/base/DataTable';
import PageHeader from '@/components/page/PageHeader';
import { Button, message, Row } from 'antd';
import DataEdit from '@/components/base/DataEdit';

import { DelClieblgm, UpCompClieblgm } from '@/services/commCRUD';
import _ from 'lodash';
import ShBlkhDgglForm from './ShBlkhDgglForm';
import Col from 'antd/es/grid/col';
import ShBlkhIndiForm from './ShBlkhIndiForm';

const daoName = 'ShBlkhDivi';
const columns: API.Column[] = [
  {
    title: '客户号',
    width: 150,
    sorter: true,
    key: 'CUNO',
    dataIndex: 'CUNO',
    dispaly: true,
    filter: true,
  },
  {
    title: '组织机构代码',
    width: 160,
    sorter: true,
    key: 'ENCD',
    dataIndex: 'ENCD',
    dispaly: true,
    filter: true,
  },
  {
    title: '客户名称',
    width: 350,
    sorter: true,
    key: 'CUNM',
    dataIndex: 'CUNM',
    dispaly: true,
    filter: true,
  },
  {
    title: '不良认定时间',
    width: 160,
    sorter: true,
    dataIndex: 'BLDT',
    key: 'BLDT',
    render: (data) => format(data, 'yyyy-MM-dd'),
    dispaly: true,
    filter: true,
  },
  {
    title: '钢贸认定时间',
    width: 160,
    sorter: true,
    key: 'GMDT',
    dataIndex: 'GMDT',
    render: (data) => format(data, 'yyyy-MM-dd'),
    dispaly: true,
    filter: true,
  },
  {
    title: '企业规模',
    width: 130,
    sorter: true,
    key: 'QYGM',
    dataIndex: 'QYGM',
    dispaly: true,
    filter: true,
  },
  {
    title: '是否不良',
    width: 130,
    sorter: true,
    dataIndex: 'BLCaption',
    key: 'ISBL',
    dispaly: true,
    filter: true,
  },
  {
    title: '是否钢贸',
    width: 130,
    sorter: true,
    key: 'ISGM',
    dataIndex: 'GMCaption',
    dispaly: true,
    filter: true,
  },
];
const controls: API.Control[] = [
  {
    name: 'CUNO',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '客户号',
    grid: 12 / 24,
  },
  {
    name: 'ENCD',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '组织机构代码',
    grid: 12 / 24,
  },
  {
    name: 'CUNM',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '客户名称',
    grid: 12 / 24,
  },
  { name: 'BLDT', label: '不良认定时间', type: 'date', grid: 23 / 24, dataType: 'date' },
  { name: 'GMDT', label: '钢贸认定时间', type: 'date', grid: 23 / 24, dataType: 'date' },
  {
    name: 'QYGM',
    rules: [{ required: true, message: '不能为空' }],
    type: 'text',
    label: '企业规模',
    grid: 12 / 24,
  },
  {
    name: 'ISBL',
    label: '是否不良',
    rules: [{ required: true, message: '不能为空' }],
    options: [
      { value: '1', label: '是' },
      { value: '0', label: '否' },
    ],
    grid: 12 / 24,
    type: 'radio-group',
  },
  {
    name: 'ISGM',
    label: '是否钢贸',
    rules: [{ required: true, message: '不能为空' }],
    options: [
      { value: '1', label: '是' },
      { value: '0', label: '否' },
    ],
    grid: 12 / 24,
    type: 'radio-group',
  },
];
const listcontrols: API.Control[] = [
  {
    name: 'DGKH',
    rules: [{ required: false, message: '不能为空' }],
    type: 'search',
    label: '对公客户',
    grid: 10 / 24,
    layout: { labelSpan: 8, wrapperSpan: 15 },
    fetch: {
      tableName: 'ShBlkhDivi',
      searchColumns: ['ENCD', 'CUNM'],
      optionTpl: 'CUNM',
      valueTpl: 'CUID',
      isDao: true,
    },
  },
  {
    name: 'ISBL',
    label: '是否不良',
    rules: [{ required: true, message: '不能为空' }],
    options: [
      { value: '1', label: '是' },
      { value: '0', label: '否' },
    ],
    grid: 12 / 24,
    type: 'radio-group',
  },
  {
    name: 'ISGM',
    label: '是否钢贸',
    rules: [{ required: true, message: '不能为空' }],
    options: [
      { value: '1', label: '是' },
      { value: '0', label: '否' },
    ],
    grid: 12 / 24,
    type: 'radio-group',
  },
];
const ShBlkhDiviForm: React.FC = () => {
  const tableRef = useRef<API.TableAction>();
  const [cuid, setCuid] = useState<number>(-1);
  const submit = async (vals: Record<string, any>) => {
    UpCompClieblgm(vals.DGKH, vals.ISBL, vals.ISGM)
      .then(function () {
        if (tableRef.current) {
          message.info('添加成功');
          tableRef.current?.reload();
        }
      })
      .catch(function (ex) {
        console.error('添加绩费参数出错', ex);
      });
  };
  return (
    <PageHeader>
      <div style={{ marginTop: -5, marginBottom: 10 }}>
        {' '}
        <DataEdit
          showType="inner"
          layout="inline"
          title="对公存款绩费系数"
          controls={listcontrols}
          onSumit={submit}
          grid={false}
          renderButtons={(p) => {
            return [
              <Button type="primary" key="submit" onClick={() => p.form?.submit?.()}>
                添加
              </Button>,
              <Button
                type="default"
                key="rest"
                onClick={() => {
                  if (cuid === -1) {
                    message.info('请选择要删除的账户');
                    return;
                  }
                  DelClieblgm(cuid)
                    .then(function () {
                      if (tableRef.current) {
                        message.info('删除成功');
                        tableRef.current?.reload();
                      }
                    })
                    .catch(function (ex) {
                      console.error('删除出错', ex);
                    });
                }}
              >
                删除
              </Button>,
            ];
          }}
        />
      </div>

      <DataTable
        daoName={daoName}
        columns={columns}
        controls={controls}
        actionRef={tableRef}
        keyColName={'CUID'}
        height={400}
        title="不良客户认定"
        pageSize={20}
        onSelectChange={async (keys) => {
          if (!_.isEmpty(keys)) {
            setCuid(keys[0] as number);
          }
        }}
      />
      <Row>
        <Col span={12}>
          <ShBlkhDgglForm CUID={cuid} />
        </Col>
        <Col span={12}>
          <ShBlkhIndiForm CUID={cuid} />
        </Col>
      </Row>
    </PageHeader>
  );
};

export default ShBlkhDiviForm;
